#include "precomp.h"

#pragma once

#include "Map.h"
#include "GameObject.h"

Map::Map(const char* filepath, const char* wall_image_path, CL_GraphicContext *gc, float tile_size) 
	: _gc(gc), _tile_size(tile_size)
{
	ifstream map_file;

	map_file.open(filepath, ifstream::in);

	string line;
	_height = 0;
	while(getline(map_file, line)){
		_map_string[_height++] = CL_String(line);
	}
	_width = _map_string[0].length();

	_character_number = 0;
	for(int cy = 0;cy < _height;cy++){
		for(int cx = 0;cx < _width;cx++){
			if(_map_string[cy][cx] >= 'A' && _map_string[cy][cx] <= 'Z'){
				_start_positions[_map_string[cy][cx] - 'A'] = CL_Point(cx, cy);
				_character_number++;
			}
		}
	}
	
	CL_SpriteDescription wall_sd;
	wall_sd.add_frame(wall_image_path);
	_sprite = new CL_Sprite(*_gc, wall_sd);
}

Map::~Map(){
	delete _sprite;
}

char Map::get_tile(int x, int y){
	return _map_string[y][x];
}

CL_Point Map::get_tile_position(float x, float y){
	return CL_Point((int)floor(x / _tile_size), (int)floor(y / _tile_size));
}

void Map::draw(float ch_x, float ch_y){
	for(int cy=0; cy < _height;cy++){
		for(int cx=0;cx < _width;cx++){
			if(_map_string[cy][cx] == MAP_WALL){
				_sprite->draw(*_gc, cx * _tile_size - ch_x + GameObject::center_x, cy * _tile_size - ch_y + GameObject::center_y);
			}
		}
	}
}